<template>
    <orh-table reload  @on-reload="refresh"
        add="zitem_add" @on-add="moralAdd" 
        del="zitem_del" @on-del="prompt = true" 
        pages :pageDate="pageDate"
        @on-page-change="queryData"
        >
        <Table :columns="columns" @on-selection-change="selectOne" :data="data" :loading="tableload" slot="table"></Table>
        <orh-sidebar v-model="show" :title="titles" scale slot="others">
            <component :is="content" :dataId="dataIds" @requery="requery"></component>
        </orh-sidebar>
        <Modal
            v-model="prompt"
            title="数据删除"
            :loading="loading"
            @on-ok="asyncOK"
            slot="others">
            <p>{{ cont }}</p>
        </Modal>
    </orh-table>
</template>
                
<script>
export default {
    components: {
        Operation: resolve => {require(['./Operation.vue'], resolve);},
    },
    data () {
        return {
            prompt:false,
            loading: true,
            cont:'您确定要删除这些数据吗',
            show:false,
            content:null,
            titles:null,
            tableload:false,
            pageDate: {
                total: 0,
                current_page: 1,
                per_page: 0
            },
            data:[],
            columns: [
                {
                    type: 'selection',
                    width: 55,
                    align: 'center',
                },
                {
                    title: '项目名称',
                    align: 'center',
                    render:(h,params)=>{
                        let name = params.row.watchlist_name.zh?params.row.watchlist_name.zh:params.row.watchlist_name.mn;
                        return h('div',name)
                    }
                },
                {
                    title: '操作',
                    key: 'action',
                    width: 200,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                directives: [
                                    {
                                        name: 'button',
                                        value: 'zitem_updata',
                                    }
                                ],
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.UpdateData(this.data[params.index].watchlist_id)
                                    }
                                }
                            }, '编辑'),
                            h('Poptip', {
                                props: {
                                    confirm: true,
                                    title: '您确定要删除这条数据吗?',
                                    type: 'error',
                                    size: 'small',
                                    placement:'left-end'
                                },
                                on: {
                                    'on-ok': () => {
                                        this.remove(params.row.watchlist_id)
                                    }
                                }
                            }, [
                                h('Button', {
                                    directives: [
                                        {
                                            name: 'button',
                                            value: 'zitem_del',
                                        }
                                    ],
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    }
                                }, '删除')
                            ]),
                        ]);
                    }
                },
            ],
            url:'api/watchlist/v1/watchlistclass/schoollist?page=',
            delUrl:'api/watchlist/v1/watchlistclass/del/',

            del:null,
            dataIds:null,
            sid:null,
        }
    },
    methods: {

        // ==================================================
        // 添加
        // ==================================================
        moralAdd() {
            this.dataIds = null
            this.titles = '添加值班项'
            this.show = true
            this.content = 'Operation'
        },

        // ==================================================
        // 单条删除
        // ==================================================
        remove (index) {
            this.del = []
            this.del.push(index)
            this.asyncOK()
        },

        // ==================================================
        // 批量删除
        // ==================================================
        selectOne(index){
            this.del = []
            index.forEach(item => {
                this.del.push(item.watchlist_id)
            });
        },

        // ==================================================
        // 执行删除
        // ==================================================
        asyncOK () {
            this.del = JSON.stringify(this.del)
            this.$http.delete(this.delUrl+this.del).then(val=>{
                this.prompt = false;
                this.queryData(this.pageDate.current_page)
            }).catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
                this.prompt = false;
            })
        },

        // ==================================================
        // 刷新数据（当数据数据发生变化）
        // ==================================================
        requery(state){
            if(state == true){
                this.show = false
                this.tableload = true
                this.queryData(this.pageDate.current_page)
            }
        },

        // ==================================================
        // 更新
        // ==================================================
        UpdateData(index){
            this.dataIds = index
            this.titles = '值班项信息修改'
            this.show = true
            this.content = 'Operation'
        },

        // ==================================================
        // 手动刷新
        // ==================================================
        refresh(){
            this.tableload = true
            this.queryData(this.pageDate.current_page)
        },

        // ==================================================
        // 请求数据
        // ==================================================
        queryData(index){
            this.tableload = true
            this.$http.get(this.url+index).then(val=>{
                this.pageDate.per_page = val.pages.per_page
                this.pageDate.total = val.pages.total
                this.pageDate.current_page = val.pages.current_page
                this.data = val.data
                this.tableload = false
            }).catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
                this.tableload = false 
            })
        },
    },
    mounted(){
        this.queryData(this.pageDate.current_page)
    },
}
</script>